package com.rmfyzxfw.caseprocess.boot.result.page;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author lixiaoxiang
 * @Description
 * @createTime 2022年02月09日 18:36:00
 */
@Data
public class Pagination<T> {
    private long page;

    private long size;
    private long total;
    private long totalPage;
    private List<T> data;

    public Pagination() {
        data = new ArrayList<>();
    }

    public Pagination(long page, long size, long total, List<T> data) {
        if (total <= 0) {
            return;
        }
        this.data = data;
        this.page = page;
        this.size = size;
        this.total = total;
        if (total % size == 0L) {
            this.totalPage = this.total / size;
        } else {
            this.totalPage = this.total / size + 1;
        }
    }

    /**
     * @param page
     * @param supplier 将DO转为VO的函数
     */
    public Pagination(Page page, Supplier<List<T>> supplier) {
        this.page = page.getCurrent();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.data = supplier.get();
    }

    /**
     * @param page
     * @param function 将DO转为VO的函数
     */
    public <R> Pagination(Page<R> page, Function<List<R>, List<T>> function) {

        this.page = page.getCurrent();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.data = function.apply(page.getRecords());
    }
    public static <T, R> Pagination<T> build(Page<R> page, Supplier<List<T>> supplier){
        return new Pagination<T>(page, supplier);
    }
    public static <T, R> Pagination<T> build(Page<R> page, Function<List<R>, List<T>> function){
        return new Pagination<T>(page, function);
    }

    public static <T> Pagination<T> build(long page, long size, long total, List<T> data){
        return new Pagination<T>(page, size, total, data);
    }

    public static Pagination empty(){
        return new Pagination();
    }
}
